<?php
!defined ('P_W') && exit('Forbidden');
require_once R_P.'lib/base/basedb.php';
class PW_BaseDB extends BaseDB{
	var $_tablename = "";
//	var $_db = null;
	var $db_perpage = 20;
	var $basename;
	var $timestamp;
	function PW_BaseDB() {
		$this->__construct();
	}
	function __construct() {
		global $db_perpage,$basename,$timestamp;
//		$this->_db = & $db;
		$this->db_perpage = & $db_perpage;
		$this->basename = $basename;
		$this->timestamp = & $timestamp;
		parent::BaseDB();
	}
	
	/**
	 * @param $basename the $basename to set
	 */
	function setBasename($basename) {
		$this->basename = $basename;
	}
	
	/**
	 * @param $db_perpage the $db_perpage to set
	 */
	function setDb_perpage($db_perpage) {
		$this->db_perpage = $db_perpage;
	}
	
	/**
	 * @param $count
	 * @param $page
	 * @return unknown_type
	 */
	function _getPages($count,$page){
		(! is_numeric ( $page ) || $page < 1 || (int)$page == 0) && $page = 1;
		$start = ($page - 1) * $this->db_perpage;
		$end = $start + $this->db_perpage - 1;
		$pages = numofpage ( $count, $page, ceil ( $count / $this->db_perpage ), $this->basename . "&" );
		$_limit = pwLimit ( ($page - 1) * $this->db_perpage, $this->db_perpage );
		return array($pages,$_limit);
	}
	
	/**
	 * 组织where查询条件
	 * @param $conditions	条件数据
	 * @param $tag			数据表引用名称
	 * @return unknown_type
	 */
	function getSqlWhere($conditions,$tag){
		$_where = "";
		if (is_array($conditions) && !empty($conditions)) {
			foreach ($conditions as $key => $value) {
				if ($value['1'] == 'like' && $value['0']) {
					$_where .= " AND ".$tag.".".$key." LIKE '%".$value['0']."%' ";
				} elseif($value['1'] == 'eq' && $value['0'] != 'all' && $value['0'] != '') {
					$_where .= " AND ".$tag.".".$key." = ".pwEscape($value['0']);
				} elseif($value['1'] == 'le'  && $value['0'] != 'all' && $value['0'] != ''){	//小于等于
					$_where .= " AND ".$tag.".".$key." <= ".pwEscape($value['0']);
				} elseif($value['1'] == 'te'  && $value['0'] != 'all' && $value['0'] != ''){	//大于等于
					$_where .= " AND ".$tag.".".$key." >= ".pwEscape($value['0']);
				} elseif($value['1'] == 'ne'  && $value['0'] != 'all' && $value['0'] != ''){	//不等于
					$_where .= " AND ".$tag.".".$key." != ".pwEscape($value['0']);
				}
			}
		}
		return $_where;
	}
	
	/**
	 * 组织where查询条件
	 * @param $conditions	条件数据
	 * @param $tag			数据表引用名称
	 * @return unknown_type
	 */
	function getSqlWhere1($conditions,$tag){
		$_where = "";
		if (is_array($conditions) && !empty($conditions)) {
			foreach ($conditions as $key => $value) {
				if ($value['1'] == 'like' && $value['0']) {
					$_where .= " AND ".$tag.".".$value[2]." LIKE '%".$value['0']."%' ";
				} elseif($value['1'] == 'eq' && $value['0'] != 'all' && $value['0'] != '') {
					$_where .= " AND ".$tag.".".$value[2]." = ".pwEscape($value['0']);
				} elseif($value['1'] == 'le'  && $value['0'] != 'all' && $value['0'] != ''){	//小于等于
					$_where .= " AND ".$tag.".".$value[2]." <= ".pwEscape($value['0']);
				} elseif($value['1'] == 'te'  && $value['0'] != 'all' && $value['0'] != ''){	//大于等于
					$_where .= " AND ".$tag.".".$value[2]." >= ".pwEscape($value['0']);
				} elseif($value['1'] == 'ne'  && $value['0'] != 'all' && $value['0'] != ''){	//不等于
					$_where .= " AND ".$tag.".".$value[2]." != ".pwEscape($value['0']);
				}
			}
		}
		return $_where;
	}
	
	
}
?>